const Health = require("../models/Health")

exports.getHealthData = async (req, res) => {
  try {
    const data = await Health.aggregate([
      { $match: { userId: req.params.userId } },
      {
        $group: {
          _id: "$type",
          records: {
            $push: {
              value: "$value",
              status: "$status",
              date: "$date",
            },
          },
        },
      },
      {
        $project: {
          _id: 0,
          type: "$_id",
          records: 1,
        },
      },
    ])

    const result = data.reduce((acc, curr) => {
      acc[curr.type] = curr.records
      return acc
    }, {})

    res.json({ code: 200, data: result })
  } catch (err) {
    res.status(500).json({ code: 500, error: err.message })
  }
}
